package com.equipment;

import java.util.NoSuchElementException;

import com.util.EncodingUtil;

/**
 * B6读车辆信息响应
 * @version 1.18
 * @author 刘亚东
 *
 */
public class ReadVehicleResponse {
	/**
	 * 测试
	 */
	public String toString() {
		return stx + rsctl + code + subCode + bcc + "内容:" + status
				+ vehiInfoLen + vehiInfoData;
	}

	// 帧开始标记
	private String stx = "55AA";
	// 串口帧序列号
	private String rsctl = "00";
	// 长度
	private String Length = "";
	// 代码类型
	private String code = "DF";
	// 拓展子码
	private String subCode = "B6";
	// 读车辆响应状态(0表示成功,非0表示失败)
	private String status = "00";
	// 车辆信息长度
	private String vehiInfoLen = "11";
	// 车辆信息内容
	private String vehiInfoData = "123456789012345678901234567890";
	// 异或校验值
	private String bcc = "e0";

	/**
	 * 解析B6读车辆信息响应
	 *
	 * @param context
	 * @return
	 */
	public ReadVehicleResponse analysis(String context) throws NoSuchElementException  {
		ReadVehicleResponse rr = null;
		try {
			// 判断是否是B6指令
			if (!context.substring(12, 14).toUpperCase().equals("B6")) {
				throw new NoSuchElementException("读车辆错误！");
			}
			// 判断状态是否成功
			if(!context.substring(14,16).equals("00")){
				throw new NoSuchElementException("读车辆错误！");
			}
			// 截取到bcc校验值 需要判断校验是否相等
			bcc = context.substring(context.length() - 2).toUpperCase();
			// 截取到发送的数据(总长度-2个字节计算到数据的最后位置)并转换下FE和FF
			String str = context.substring(4, context.length() - 2);
			// 得到比对校验
			str = EncodingUtil.yihuo(EncodingUtil.change(str)).toUpperCase();
			// 判断校验是否一致
			if (!str.equals(bcc)) {
				throw new NoSuchElementException("读车辆信息异或错误!");
			}
			// 如果没问题就new 对象 把解析的值放入对象
			rr = new ReadVehicleResponse();
			// 截取到串口帧序列号
			rsctl = context.substring(4, 6);
			rr.setRsctl(rsctl);
			// 解析长度
			Length = context.substring(6, 10);
			rr.setLength(Length);
			// 解析指令代码
			code = context.substring(10,12);
			rr.setCode(code);
			// 解析拓展子代码
			subCode = context.substring(12, 14);
			rr.setSubCode(subCode);
			// 解析读车辆响应状态
			status = context.substring(14, 16);
			rr.setStatus(status);
			// 解析车辆信息长度
			vehiInfoLen = context.substring(16, 18);
			rr.setVehiInfoLen(vehiInfoLen);
			// 解析车辆信息内容
			vehiInfoData = context.substring(18, context.length() - 2);
			rr.setVehiInfoData(vehiInfoData);
		}catch (NoSuchElementException e) {
			throw new NoSuchElementException(e.getMessage());
		} catch (Exception e) {

		}
		return rr;
	}

	public String getStx() {
		return stx;
	}

	public void setStx(String stx) {
		this.stx = stx;
	}

	public String getRsctl() {
		return rsctl;
	}

	public void setRsctl(String rsctl) {
		this.rsctl = rsctl;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getSubCode() {
		return subCode;
	}

	public void setSubCode(String subCode) {
		this.subCode = subCode;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getVehiInfoLen() {
		return vehiInfoLen;
	}

	public void setVehiInfoLen(String vehiInfoLen) {
		this.vehiInfoLen = vehiInfoLen;
	}

	public String getVehiInfoData() {
		return vehiInfoData;
	}

	public void setVehiInfoData(String vehiInfoData) {
		this.vehiInfoData = vehiInfoData;
	}

	public String getBcc() {
		return bcc;
	}

	public void setBcc(String bcc) {
		this.bcc = bcc;
	}

	public String getLength() {
		return Length;
	}

	public void setLength(String length) {
		Length = length;
	}

}
